//
//  CommunityViewController.m
//  BeautyClother
//
//  Created by dllo on 15/8/21.
//  Copyright (c) 2015年 宋爽. All rights reserved.
//
#import "DKNightVersion.h"

#import "CommunityViewController.h"
#import "TopicViewController.h"
#import "myCell.h"
#import "AsyncConnection.h"
#import "define.h"
#import "UIImageView+WebCache.h"
#import "wareLayout.h"
#import "wareCell.h"
#import "goodCell.h"
#import "CircleCell.h"
#import "OneImageCell.h"
#import "ThreeCell.h"
#import "ImageWithTitle.h"
#import "MJRefresh.h"
#import "AVPlayCell.h"
#import "HotDetailViewController.h"
#import "StudyDetailViewController.h"
#import "WebController.h"
#import "MagazineDetailViewController.h"
#import "Reachability.h"
#import "AVPlayController.h"
#import "MBProgressHUD.h"
#import "LeftViewController.h"
int i = 0;

@interface CommunityViewController ()<UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UIScrollView *bigScroll;
@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) UITableView *hotTable;
@property (nonatomic, retain) UITableView *studyTable;
@property (nonatomic, retain) UITableView *leftTable;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) NSMutableArray *wareArr;
@property (nonatomic, retain) NSMutableArray *fashArr;
@property (nonatomic, retain) NSMutableArray *hotArr;
@property (nonatomic, retain) NSMutableArray *leftArr;
@property (nonatomic, retain) NSMutableArray *moreArr;
@property (nonatomic, retain) NSMutableArray *imageArr;
@property (nonatomic, retain) UIScrollView *scroll;
@property (nonatomic, retain) UIScrollView *magazine;
@property (nonatomic, retain) NSTimer *timer;
@property (nonatomic, retain) UIPageControl *page;

@property (nonatomic, retain) NSMutableArray *flag;

@property (nonatomic, assign) NSInteger pagenum;

@property (nonatomic, assign) CGFloat offWidth;
@property (nonatomic, assign) CGFloat offHeight;
@property (nonatomic, retain) MBProgressHUD *mb;

@end

@implementation CommunityViewController

- (void)dealloc
{
    [_scroll release];
    [_magazine release];
    [_imageArr release];
    [_moreArr release];
    [_leftTable release];
    [_leftArr release];
    [_hotArr release];
    [_fashArr release];
    [_array release];
    [_wareArr release];
    [_table release];
    [_hotTable release];
    [_studyTable release];
    [_bigScroll release];
    [_flag release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.wareArr = [NSMutableArray array];
        self.fashArr = [NSMutableArray array];
        self.hotArr = [NSMutableArray array];
        self.leftArr = [NSMutableArray array];
        self.moreArr = [NSMutableArray array];
        self.imageArr = [NSMutableArray array];
        self.flag = [NSMutableArray array];
        self.flag = @[@"1440460800.0", @"1440302400.0", @"1440108000.0",@"1439901000.0", @"1439787457.0", @"1439625600.0", @"1439416800.0", @"1439251200.0"].mutableCopy;
        self.offWidth = [[UIScreen mainScreen]bounds].size.width / 375;
        self.offHeight = [[UIScreen mainScreen]bounds].size.height / 667;
        self.leftArr = @[[UIColor redColor], [UIColor orangeColor], [UIColor yellowColor], [UIColor greenColor], [UIColor colorWithRed:0 green:0 blue:1 alpha:0.2], [UIColor blueColor], [UIColor purpleColor], [UIColor blackColor], @"Red", @"Orange", @"Yellow", @"Green", @"Blue", @"Indigo", @"Purple", @"B&W"].mutableCopy;
    }
    return self;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self hotConnection];
    
    [self createNaviButton];
    [self createBigScroll];
    [self createLeftTable];
    [self createHotTable];
    [self createTableView];
    [self createStudyTable];
    self.automaticallyAdjustsScrollViewInsets = NO;
    BOOL dd = [CommunityViewController judgeConnect];
    if (dd) {
        self.mb = [[MBProgressHUD alloc]initWithView:[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]];
        [self.view addSubview:self.mb];
        self.mb.labelText = @"努力加载着";
        [self.mb show:YES];
    }
    
    
    
    
    // Do any additional setup after loading the view.
}


- (void)addHeader
{
    __block CommunityViewController *actionVC = self;
    
    [self.table addHeaderWithCallback:^{
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [actionVC.table headerEndRefreshing];
            
        });
        [self.table headerEndRefreshing];
    }];
    
    
    [self.hotTable addHeaderWithCallback:^{
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           
            [actionVC.hotTable headerEndRefreshing];
            
        });
        [self.hotTable headerEndRefreshing];
    }];
    
    
}

- (void)addFooter
{
    self.pagenum = 2;
    __block CommunityViewController *actionVC = self;
    
    [self.table addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            NSDictionary *dic = @{@"type": @"hot",
                                  @"flag": [self.flag objectAtIndex:i],
                                  
            
                                  @"gf":@"iphone",
                                  @"gv":@"6.3.0",
                                  @"access_token":@""};
            
            [AsyncConnection asyncConnection:COMM_TOPIC parmaters:dic block:^(NSData *data) {
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                NSMutableArray *arr = [NSMutableArray array];
                arr = [[dic objectForKey:@"data"]objectForKey:@"items"];
                [self.array addObjectsFromArray:arr];
                
                NSLog(@"%ld", self.array.count);
                
                [self.table reloadData];
                
            }path:@"comm_topic_2"];
            
            i++;
            [actionVC.table footerEndRefreshing];
        });
    }];
    
    
    
    [self.hotTable addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            NSDictionary *dic = @{@"lang": @"en",
                                  @"ag":@"",
                                  @"page":[NSString stringWithFormat:@"%ld", self.pagenum],
                                  @"token":@"1416b90f03d637a168f695105aa4132d7",
                                  @"ua":@"iPhone%7C1.7.11",
                                  @"sig":@"5fad08f2c4f51440988427742",
                                  @"nt":@"wifi"};
            [AsyncConnection asyncConnection:COMM_HOT parmaters:dic block:^(NSData *data) {
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                NSMutableArray *arr = [NSMutableArray array];
                arr = [dic objectForKey:@"sets"];
                [self.moreArr addObjectsFromArray:arr];
                
                NSLog(@"%ld", self.moreArr.count);
                
                [self.hotTable reloadData];
                
                
            }path:@"comm_hot2"];
            
            self.pagenum = _pagenum + 1;
            [actionVC.hotTable footerEndRefreshing];
        });
    }];

    
    
}




#pragma mark - 
#pragma mark 数据解析

- (void)hotConnection
{
    
    NSDictionary *dic = @{@"lang": @"en",
                          @"ag":@"",
                          @"page":@"1",
                          @"token":@"1416b90f03d637a168f695105aa4132d7",
                          @"ua":@"iPhone%7C1.7.11",
                          @"sig":@"5fad08f2c4f51440988427742",
                          @"nt":@"wifi"};
    [AsyncConnection asyncConnection:COMM_HOT parmaters:dic block:^(NSData *data) {
//        [data writeToFile:path atomically:YES];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *arr = [NSMutableArray array];
        arr = [dic objectForKey:@"sets"];
        [self.moreArr addObjectsFromArray:arr];
       
        
        [self.hotTable reloadData];
        
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 150 * _offWidth, 60 *_offHeight)];
        
        image.center = self.view.center;
        [self.view addSubview:image];
        image.layer.cornerRadius = 5;
        image.layer.masksToBounds = YES;
        image.backgroundColor = [UIColor lightGrayColor];
        [image release];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150 * _offWidth, 60 *_offHeight)];
        label.text = @"向左滑有惊喜";
        
        label.textAlignment = 1;
        [image addSubview:label];
        label.textColor = [UIColor whiteColor];
        [label release];
        [UIView animateWithDuration:1.5 animations:^{
            
            image.alpha = 0.2;
            
        } completion:^(BOOL finished) {
            [image removeFromSuperview];
        }];

        [self.mb removeFromSuperview];

        [self beginConnection];

    }path:@"comm_hot"];
    
}

- (void)beginConnection
{
    
    
    NSDictionary *dic = @{@"category":@"%E5%85%A8%E9%83%A8",
                          @"flag":@"",
                          @"gv":@"6.3.0",
                          @"gf":@"iphone",
                          @"gs":@"640x1136",
                          };
    
    [AsyncConnection asyncConnection:COMM_TOPIC parmaters:dic block:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.array = [[dic objectForKey:@"data"]objectForKey:@"items"];
        
        
        
        
        [self.table reloadData];
        
        [self connectionStudy];
    }path:@"comm_topic"];
}


- (void)tuijianConnection
{

    [AsyncConnection asyncConnection:Study_List parmaters:nil block:^(NSData *data) {
        NSDictionary *fashionDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *arr1 = [fashionDic objectForKey:@"list"];
        [self.hotArr addObjectsFromArray:arr1];
        
        [self createStudyTable];
        [self.studyTable reloadData];
        
    }path:@"comm_tuijian"];

}

- (void)fashionConnection
{
    NSDictionary *fash = @{@"type":@"daily"};
    [AsyncConnection asyncConnection:COMM_WARE parmaters:fash block:^(NSData *data) {
        NSDictionary *fashionDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *arr1 = [[fashionDic objectForKey:@"data"]objectForKey:@"items"];
        [self.fashArr addObjectsFromArray:arr1];
        [self tuijianConnection];
    }path:@"comm_ware"];
}

- (void)connectionStudy
{
    
    NSDictionary *ware = @{@"type":@"style"};
    [AsyncConnection asyncConnection:COMM_WARE parmaters:ware block:^(NSData *data) {
        NSDictionary *wareDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *arr = [[wareDic objectForKey:@"data"]objectForKey:@"items"];
        if (arr.count != 0) {
            
        
        [self.wareArr addObject:[arr lastObject]];
        [self.wareArr addObjectsFromArray:arr];
        [self.wareArr addObject:[arr firstObject]];
        NSLog(@"%lud",(unsigned long) self.wareArr.count);
        }
        [self fashionConnection];
        
    
   }path:@"comm_ware_style"];
    
}





+ (BOOL)judgeConnect
{
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    NetworkStatus sta = [reach currentReachabilityStatus];
    if (sta == NotReachable) {
        return NO;
    }else{
        return YES;
    }
}

#pragma mark -
#pragma mark 最下面一个scrollview

- (void)createBigScroll
{
    self.bigScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:self.bigScroll];
    self.bigScroll.contentSize = CGSizeMake(self.view.frame.size.width * 3, 0);
    self.bigScroll.pagingEnabled = YES;
    self.bigScroll.delegate = self;
    [self.bigScroll release];
    
}

#pragma mark -
#pragma mark - navigation上的button

- (void)createNaviButton
{
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    title.font = [UIFont systemFontOfSize:17];

    title.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = title;

    NSArray *arr = @[@"热门", @"专题", @"教程"];
    for (int i = 0; i < 3; i ++) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(i * 70 *_offWidth + 100 *_offWidth, 5 *_offHeight, 50*_offWidth, 40 *_offHeight)];

        [button setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];

        [button addTarget:self action:@selector(next:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = i + 100;
//        [self.navigationController.navigationBar addSubview:button];
        [title addSubview:button];
    
        if (button.tag == 100) {
            [button setTitleColor:[UIColor colorWithRed:1 green:0.5 blue:0.5 alpha:1] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont boldSystemFontOfSize:20];

        }
        
//        [button release];
    }
}

- (void)next:(UIButton *)button
{
    
    self.bigScroll.contentOffset = CGPointMake((button.tag - 100) * self.bigScroll.frame.size.width, 0);
    NSArray *arr = self.navigationController.navigationBar.subviews;
    for (UIButton *view in arr) {
        
        if ([view isKindOfClass:[UIButton class]]) {
            if (view.tag == button.tag) {
                
            
            [view setTitleColor:[UIColor colorWithRed:255 / 255 green:0 / 255 blue:255 / 255 alpha:1] forState:UIControlStateNormal];
                view.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        }
        else
        {
            [view setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            view.titleLabel.font = [UIFont systemFontOfSize:17];

        }
        }
    }
}

#pragma mark -
#pragma mark - 热门

- (void)createHotTable
{
    self.hotTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 112)];
    self.hotTable.delegate = self;
    self.hotTable.dataSource = self;
    [self.bigScroll addSubview:self.hotTable];
    [self.hotTable release];
    

    
}




- (void)createLeftTable
{
    self.leftTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 80*_offHeight, 300 *_offWidth, self.view.frame.size.height - 80 *_offHeight) style:UITableViewStylePlain];
    self.leftTable.delegate = self;
    self.leftTable.dataSource = self;
    [self.leftTable release];
}


- (void)swip:(UISwipeGestureRecognizer *)swip
{
    [[self.view viewWithTag:111]removeFromSuperview];
    self.navigationController.navigationBar.hidden = NO;
    self.bigScroll.userInteractionEnabled = YES;
}




- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == self.bigScroll) {
        
            NSArray *arr = self.navigationItem.titleView.subviews;
            for (UIButton *button in arr) {
                if ([button isKindOfClass:[UIButton class]]) {
                    
                
                if (button.tag == scrollView.contentOffset.x / self.view.frame.size.width + 100) {
                    [button setTitleColor:[UIColor colorWithRed:1 green:0.5 blue:0.5 alpha:1] forState:UIControlStateNormal];
                    button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
                }
                else
                {
                    [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
                    button.titleLabel.font = [UIFont systemFontOfSize:17];
                }
                }
            
        }
    }
    
    if (scrollView == self.magazine) {
        if (scrollView.contentOffset.x == self.magazine.frame.size.width * (self.wareArr.count - 1)) {
            
            scrollView.contentOffset = CGPointMake(self.magazine.frame.size.width, 0);
        }
        if (scrollView.contentOffset.x == 0) {
            scrollView.contentOffset = CGPointMake(self.magazine.frame.size.width * (self.wareArr.count - 2), 0);
        }
    }
    
}

- (void)createLeftSlide
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 75, self.view.frame.size.height)];
    view.backgroundColor = [UIColor yellowColor];
    view.tag = 111;
    UILabel *text = [[UILabel alloc]initWithFrame:CGRectMake(20 *_offWidth, 30*_offHeight, view.frame.size.width - 40 *_offWidth, 40 *_offHeight)];
    [view addSubview:text];
    
    text.text = @"选择颜色";
    text.textAlignment = 1;
    text.font = [UIFont systemFontOfSize:20];

    
    [view addSubview:_leftTable];
    
    
    [self.view addSubview:view];
    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swip:)];
    [view addGestureRecognizer:swip];
    swip.direction = UISwipeGestureRecognizerDirectionLeft;
    
    self.navigationController.navigationBar.hidden = YES;
    self.bigScroll.userInteractionEnabled = NO;
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == self.bigScroll) {
         if (scrollView.contentOffset.x <= 0)
         {
    
        [self createLeftSlide];
        
    }
    }

}


#pragma mark -
#pragma mark - 专题
#pragma mark 整个一个大的tableview

- (void)createTableView
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(self.bigScroll.frame.size.width, 64, self.view.frame.size.width, self.view.frame.size.height - 112) style:UITableViewStylePlain];
    self.table.backgroundColor = [UIColor lightTextColor];
    [self.bigScroll addSubview:self.table];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.table release];

    
    [self addFooter];
    [self addHeader];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.studyTable) {
        
        if (section == 0) {
            return 2;
        }
        if (section == 1) {
            return self.hotArr.count;
        }
        // 其它两个表返回行数
    }if (tableView == _leftTable) {
        
        return self.leftArr.count / 2;
    }else
    {
        if (self.moreArr.count == 0) {
            return 0;
        }else {
            
            return 1;
            
        }
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.table) {
    myCell *cell = [tableView dequeueReusableCellWithIdentifier:@"eee"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (cell == nil) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell = [[myCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"eee"];
        
    }
        
#pragma mark 图片截取
        cell.backgroundColor = [UIColor clearColor];
        NSDictionary *dic = [[self.array objectAtIndex:indexPath.section]objectForKey:@"component"];
        NSString *pic = [[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"picUrl"];
        NSArray *arr = [pic componentsSeparatedByString:@"?"];
        
        [cell.image sd_setImageWithURL:[NSURL URLWithString:[arr objectAtIndex:0]] placeholderImage:nil];
        
        cell.source.text = [[@"#"stringByAppendingString:[dic objectForKey:@"category"]]stringByAppendingString:@"#"];
        cell.title.text = [dic objectForKey:@"title"];
        [cell.date setTitle:[[[dic objectForKey:@"year"]stringByAppendingFormat:@"%@%@", @"." ,[dic objectForKey:@"month"]]stringByAppendingFormat:@"%@%@", @".", [dic objectForKey:@"month"]] forState:UIControlStateNormal];
    [cell.comment setTitle:[dic objectForKey:@"commentCount"] forState:UIControlStateNormal];
    [cell.like setTitle:[dic objectForKey:@"collectionCount"] forState:UIControlStateNormal];
    
    
    return cell;
    }
    
    if (tableView == self.hotTable) {
        
        NSDictionary *dic = [self.moreArr objectAtIndex:indexPath.section];
        NSArray *arr = @[[dic objectForKey:@"url1"], [dic objectForKey:@"url2"], [dic objectForKey:@"url3"]];
            ThreeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ooo"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if (cell == nil) {
                cell = [[ThreeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ooo"];
            }
            
            [cell sendPhoto:arr];
            cell.title.text = [dic objectForKey:@"title"];
            cell.name.text = [dic objectForKey:@"desc"];
        
        
            return cell;
        
    }
    if (tableView == _leftTable) {
        
       
        UITableViewCell *viewcell = [tableView dequeueReusableCellWithIdentifier:@"jjj"];
        viewcell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (viewcell == nil) {
            viewcell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"jjj"];
            viewcell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
            
            

            UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(20 *_offWidth, 5 *_offHeight, 50 *_offWidth, 50 *_offHeight)];
                image.layer.cornerRadius = 50 *_offWidth / 2;
        
            [viewcell.contentView addSubview:image];
                image.backgroundColor = [self.leftArr objectAtIndex:indexPath.row];

            image.alpha = 0.5;
            UILabel *texts = [[UILabel alloc]initWithFrame:CGRectMake(80 *_offWidth, 5 *_offHeight, viewcell.frame.size.width - 60 *_offWidth, 50 *_offHeight)];
            [viewcell addSubview:texts];
            texts.text = [self.leftArr objectAtIndex:indexPath.row + 8];
            
        }
        
        
        return viewcell;
    }
    if (tableView == self.studyTable) {
        
        if (indexPath.section == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sss"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if (cell == nil) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"sss"];
            }
            if (indexPath.row == 0) {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.textLabel.text = @"穿搭志";
        
            }else
            {
                [self createScroll];
                [cell.contentView addSubview:self.magazine];

            }
            return cell;
        }
        
        else {
            AVPlayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ssss"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if (cell == nil) {
                cell = [[AVPlayCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"ssss"];
            }
            NSString *string = [NSString stringWithFormat:@"http://v.meilapp.com:8080%@",[[self.hotArr objectAtIndex:indexPath.row] objectForKey:@"image_url"]];
            [cell.image sd_setImageWithURL:[NSURL URLWithString:string] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
            cell.video_time.text = [[self.hotArr objectAtIndex:indexPath.row] objectForKey:@"video_time"];
            cell.title.text = [[self.hotArr objectAtIndex:indexPath.row] objectForKey:@"title"];
            [cell.addtime setImage:[UIImage imageNamed:@"date.png"] forState:UIControlStateNormal];
            [cell.addtime setTitle:[[self.hotArr objectAtIndex:indexPath.row] objectForKey:@"addtime"] forState:UIControlStateNormal];
            cell.tag_name.text = [[[[self.hotArr objectAtIndex:indexPath.row] objectForKey:@"tagList"] objectAtIndex:0] objectForKey:@"tag_name"];
            
            cell.image.userInteractionEnabled = YES;
            
            UITapGestureRecognizer *tap  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
            [cell.image addGestureRecognizer:tap];
            cell.image.tag = indexPath.row;
            return cell;
        }
    }
    else
    {
        return [[[UITableViewCell alloc] init] autorelease];
    }

}

- (void)tap:(UITapGestureRecognizer *)tap
{
    AVPlayController *AVplay = [[AVPlayController alloc] init];
    AVplay.url = [[self.hotArr objectAtIndex:tap.view.tag] objectForKey:@"url"];
     NSString *string = [NSString stringWithFormat:@"http://v.meilapp.com:8080%@",[[self.hotArr objectAtIndex:tap.view.tag] objectForKey:@"image_url"]];
    AVplay.hidesBottomBarWhenPushed = YES;
    AVplay.ImageURL = string;
    [self.navigationController pushViewController:AVplay animated:YES];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.leftTable) {
        LeftViewController *left = [[LeftViewController alloc]init];
        left.col = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
        
        [self.navigationController pushViewController:left animated:YES];
        [left release];
    }
    if (tableView == self.table) {
        TopicViewController *top = [[TopicViewController alloc]init];
        top.tid = [[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"id"];
        top.title = [[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"title"];
        top.image = [[[self.array objectAtIndex:indexPath.section]objectForKey:@"component"]objectForKey:@"picUrl"];
        top.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:top animated:YES];
        [top release];
    }
    if (tableView == self.hotTable) {
        HotDetailViewController *hotDetail = [[HotDetailViewController alloc]init];
        hotDetail.hidesBottomBarWhenPushed = YES;
        hotDetail.hid = [[self.moreArr objectAtIndex:indexPath.section]objectForKey:@"setId"];
        hotDetail.pid = [[self.moreArr objectAtIndex:indexPath.section]objectForKey:@"publisherId"];
        hotDetail.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:hotDetail animated:YES];
        [hotDetail release];
    }
    if (tableView == self.studyTable) {
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                MagazineDetailViewController *maza = [[MagazineDetailViewController alloc]init];
                maza.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:maza animated:YES];
                [maza release];
            }
        }
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView == self.hotTable) {
        if (section == 0) {
            return 5 *_offHeight;
        }else
        {
            return 10 *_offHeight;
        }
        
    }else
    {
        return 5 *_offHeight;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.table) {
        return 200 *_offHeight;
    }
    if (tableView == self.hotTable) {
        return 370 *_offHeight;
    }
    if (tableView == self.studyTable) {
        
            if (indexPath.section == 0) {
                if (indexPath.row == 0) {
                    return 40 * _offHeight;
                }
                else{
                    return 300 *_offHeight;
                }
            }
            if (indexPath.section == 1) {
                return 320 *_offHeight;
            }
    }
    else
    {
        return 60 * _offHeight;
    }
    return 0;
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.studyTable) {
        if (self.hotArr.count ==0) {
            return 0;
        }else
        {
        return 2;
        }
    }
    if (tableView == self.table) {
        return self.array.count;
    }
    if (tableView == self.hotTable) {
        return self.moreArr.count;
    }
    else
    {
        return 1;
    }
    
}


#pragma mark - 
#pragma mark - 教程

- (void)createStudyTable
{
    self.studyTable = [[UITableView alloc]initWithFrame:CGRectMake(self.bigScroll.frame.size.width * 2, 64, self.view.frame.size.width, self.view.frame.size.height - 112) style:UITableViewStylePlain];
    [self.bigScroll addSubview:self.studyTable];
    self.studyTable.delegate = self;
    self.studyTable.dataSource = self;
    [self.studyTable release];
}

- (void)createScroll
{
    self.magazine = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300 *_offHeight)];
    
    _magazine.delegate = self;
   
    _magazine.contentSize = CGSizeMake(self.wareArr.count * _magazine.frame.size.width, 0);
    _magazine.pagingEnabled = YES;
    _magazine.contentOffset = CGPointMake(self.magazine.frame.size.width, 0);
    [_magazine release];
    [self createImage];
    
    
}

- (void)createImage
{
    for (int i = 0; i < self.wareArr.count; i++) {
        UIImageView *iamge = [[UIImageView alloc]initWithFrame:CGRectMake(i * self.view.frame.size.width + 30 * _offWidth, 0, self.view.frame.size.width - 60 *_offWidth, self.magazine.frame.size.height)];
        
        [self.magazine addSubview:iamge];
        iamge.tag = i;
        iamge.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(wareNext:)];
        [iamge addGestureRecognizer:tap];
        [tap release];
        [iamge sd_setImageWithURL:[NSURL URLWithString:[[[self.wareArr objectAtIndex:i]objectForKey:@"component"]objectForKey:@"picUrl"]] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
        [iamge release];
    }
}

- (void)wareNext:(UITapGestureRecognizer *)tap
{
    WebController *wareWeb = [[WebController alloc]init];
    NSString *str = [@"http://api2.hichao.com/theme?id=" stringByAppendingString:[[[[self.wareArr objectAtIndex:tap.view.tag]objectForKey:@"component"]objectForKey:@"action"]objectForKey:@"id"]];
    wareWeb.hidesBottomBarWhenPushed = YES;
    NSURL *url = [NSURL URLWithString:str];
    wareWeb.url = url;
    [self.navigationController pushViewController:wareWeb animated:YES];
    [wareWeb release];
    

}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

    @end
